<template>
  <el-card class="box-card">
    <div>
      <!-- 搜索框 -->
      <el-form v-show="showSearch" ref="searchData" size="small" :inline="true" :model="searchData" class="demo-form-inline">
        <el-form-item label="用户名" style="width: 23%">
          <el-input v-model="searchData.username" placeholder="用户名" clearable maxlength="64"/>
        </el-form-item>
        <el-form-item label="账号" style="width: 23%">
          <el-input v-model="searchData.account" placeholder="账号" clearable maxlength="64"/>
        </el-form-item>
        <el-form-item label="姓名" style="width: 23%">
          <el-input v-model="searchData.name" placeholder="姓名" clearable maxlength="64"/>
        </el-form-item>
        <el-form-item label="邮箱" style="width: 23%">
          <el-input v-model="searchData.email" placeholder="邮箱" clearable maxlength="64"/>
        </el-form-item>
        <el-form-item label="手机号" style="width: 23%">
          <el-input v-model="searchData.mobile" placeholder="手机号" clearable maxlength="64"/>
        </el-form-item>
        <el-form-item label="状态" style="width: 23%">
          <el-select v-model="searchData.status" clearable placeholder="状态">
            <el-option label="正常" value="正常"></el-option>
            <el-option label="禁用" value="禁用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别" style="width: 23%">
          <el-select v-model="searchData.sex" clearable placeholder="性别">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="getDataListBySearch">查询</el-button>
          <el-button icon="el-icon-refresh" @click="refresh">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 按钮 -->
      <el-row :gutter="10">
        <el-col :span="1.5">
          <printTable :data="getPrintData" :properties="properties"/>
        </el-col>

        <right-toolbar :columns="properties" :showSearch.sync="showSearch" @queryTable="getDataList"></right-toolbar>
      </el-row>

      <!-- 角色数据表单 -->
      <el-table :data="dataList" border>
        <el-table-column
	        v-for="{prop, label, width} in getProp"
	        :key="prop"
	        :label="label"
	        :prop="prop"
	        :width="width"
	        sortable>
        </el-table-column>
	      <el-table-column label="操作" width="120">
          <template v-slot="scope">
            <el-button size="mini" type="primary" @click="handleLogin(scope.row.id)">
              虚拟登录
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 页号 -->
      <el-pagination
        :current-page="pageIndex"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="totalPage"
        layout="total ,sizes,prev,pager,next,jumper"
        style="margin-top: 30px"
        @size-change="sizeChangeHandle"
        @current-change="CurrentChangeHandle"
      >
      </el-pagination>
    </div>
  </el-card>
</template>
<script>


export default {
  name: "SysVirtualLogin",
  data() {
    return {
      //数据参数
      searchData: {},//搜索数据

      //集合参数
      dataList: [], //页面展示的数据集合

      //页号参数
      pageIndex: 1,
      pageSize: 5,
      pageSizes: [],
      totalPage: 0,
      begin: 0,
      end: this.pageSize - 1,

      //其他
      showSearch: true,// 显示搜索条件
      dataListFrom: "getDataList",//当前数据来源于搜索还是全局

      //表头数据
      properties: [],
    };
  },
  methods: {
    //改变数组大小
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.SizeOrCurrentChangeAfter();
    },
    //改变当前页号
    CurrentChangeHandle(val) {
      this.pageIndex = val;
      this.SizeOrCurrentChangeAfter();
    },
    //在更改数组大小或者页号前，判断数据来源
    SizeOrCurrentChangeAfter() {
      if (this.dataListFrom === "getDataList")
        this.getDataList();
      else this.getDataListBySearch();
    },

    //查询所有数据
    getDataList() {
      this.dataListFrom = "getDataList";
      const pageInfo = this.$getPageInfo(this.pageSize, this.pageIndex)
      this.$http.post("/user/getPageVo", pageInfo).then((res) => {
        this.cutDataList(res);
      });
    },
    //查询单条数据
    getDataListBySearch() {
      this.dataListFrom = "getDataListBySearch";
      const data = {
        pageBo: this.$getPageInfo(this.pageSize, this.pageIndex),
        userBo: this.searchData
      }
      this.$http.post("/user/getPageVo/search", data).then((res) => {
        this.cutDataList(res);
      });
    },

    //分页操作
    cutDataList(res) {
      const data = this.$cutPageDataList(res)
      this.dataList = data.dataList
      this.totalPage = data.totalPage
    },

    //虚拟登录
    handleLogin(item) {
      this.$http.post("/login/virtualLogin/" + item).then((res) => {
        if(res === null){
          return;
        }
        const {href} = this.$router.resolve({
          path: "/",
          query: {
            "token": res["token"],
            "username": res["username"],
            "account": res["account"],
            "tokenName": res["tokenName"]
          }
        });
        window.open(href, '_blank');
      });
    },
    //刷新页面
    refresh() {
      this.$GlobalLoading(true, {text: "正在重置搜索条件，请稍候..."});
      setTimeout(() => {
        this.searchData = {};
        this.getDataList();
        this.$GlobalLoading(false, null);
      }, 1000);
    },

    //初始化页号
    setPageSize() {
      this.pageSizes = this.$getPageSize()
      this.pageSize = this.pageSizes[0]
    },

    //获取表头
    getProperties() {
      this.properties = this.$getProp("/prop/user")
    }
  },
  mounted() {
    this.getProperties();
	  this.setPageSize();
    this.getDataList();
  },
  computed: {
    //获取打印集合
    getPrintData() {
      return JSON.parse(JSON.stringify(this.dataList));
		},
		//获取表头
		getProp() {
			return this.properties.filter(item => item.checked);
		}
  },
};
</script>
